library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(knitr)
library(cowplot)
library(latex2exp)
## Warning: package 'latex2exp' was built under R version 4.0.5
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:latex2exp':
## 
##     TeX
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
 library(gapminder)
 library("png")
 library("RCurl")
## Warning: package 'RCurl' was built under R version 4.0.5
## 
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
## 
##     complete
library("gifski")
library("grDevices")
library("gganimate")
library("ggridges")
#reading in data sets
countries_total <- read_csv("countries_total.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   name = col_character(),
##   `alpha-2` = col_character(),
##   `alpha-3` = col_character(),
##   `country-code` = col_double(),
##   `iso_3166-2` = col_character(),
##   region = col_character(),
##   `sub-region` = col_character(),
##   `intermediate-region` = col_character(),
##   `region-code` = col_double(),
##   `sub-region-code` = col_double(),
##   `intermediate-region-code` = col_double()
## )
income_per_person <- read_csv("income_per_person.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   geo = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
life_expectancy_years <- read_csv("life_expectancy_years.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   geo = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
population_total <- read_csv("population_total.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   geo = col_character()
## )
## ℹ Use `spec()` for the full column specifications.

Changing income_per_person data set to longitudinal data set.

#head(countries_total)
#head(population_total)
#head(life_expectancy_years)
L_income_person <- income_per_person %>%
gather(key = "year", value = "Income",
2:220)
#head(L_income_person)
#head(life_expectancy_years)
#longitudinal data such that the resulting data set has three columns for the data sets required
L_life_expect <- life_expectancy_years %>%
gather(key = "year", value = "LifeExp",
2:220)
head(L_life_expect)
## # A tibble: 6 x 3
##   geo                 year  LifeExp
##   <chr>               <chr>   <dbl>
## 1 Afghanistan         1800     28.2
## 2 Albania             1800     35.4
## 3 Algeria             1800     28.8
## 4 Andorra             1800     NA  
## 5 Angola              1800     27  
## 6 Antigua and Barbuda 1800     33.5

Merging the two longitudinal data sets.

Life_expect_income<- merge(L_income_person,L_life_expect,
by.x = 
c("geo","year"),
by.y = c("geo","year"))
head(Life_expect_income)
##           geo year Income LifeExp
## 1 Afghanistan 1800    603    28.2
## 2 Afghanistan 1801    603    28.2
## 3 Afghanistan 1802    603    28.2
## 4 Afghanistan 1803    603    28.2
## 5 Afghanistan 1804    603    28.2
## 6 Afghanistan 1805    603    28.2

Merging life expeect and income with the countries data.

#Merging life expeect and income with the countries data.

head(countries_total)
## # A tibble: 6 x 11
##   name      `alpha-2` `alpha-3` `country-code` `iso_3166-2`  region `sub-region`
##   <chr>     <chr>     <chr>              <dbl> <chr>         <chr>  <chr>       
## 1 "Afghani… AF        AFG                    4 ISO 3166-2:AF Asia   Southern As…
## 2 "\xea\xf… AX        ALA                  248 ISO 3166-2:AX Europe Northern Eu…
## 3 "Albania" AL        ALB                    8 ISO 3166-2:AL Europe Southern Eu…
## 4 "Algeria" DZ        DZA                   12 ISO 3166-2:DZ Africa Northern Af…
## 5 "America… AS        ASM                   16 ISO 3166-2:AS Ocean… Polynesia   
## 6 "Andorra" AD        AND                   20 ISO 3166-2:AD Europe Southern Eu…
## # … with 4 more variables: intermediate-region <chr>, region-code <dbl>,
## #   sub-region-code <dbl>, intermediate-region-code <dbl>
countries_total <- countries_total %>%
  select(name,region)
life_ex_income <- merge(Life_expect_income,countries_total, by.x = "geo",
                       by.y = "name")
head(life_ex_income)
##           geo year Income LifeExp region
## 1 Afghanistan 1824    613    28.0   Asia
## 2 Afghanistan 1825    615    27.9   Asia
## 3 Afghanistan 1888    761    28.5   Asia
## 4 Afghanistan 1826    617    27.9   Asia
## 5 Afghanistan 1886    756    28.4   Asia
## 6 Afghanistan 1887    759    28.5   Asia

Converting pop_total data set to longitudinal dataset.

L_populationT <- population_total %>%
  gather(key = "year", value = "Population",
2:220)


head(L_populationT)
## # A tibble: 6 x 3
##   geo                 year  Population
##   <chr>               <chr>      <dbl>
## 1 Afghanistan         1800     3280000
## 2 Albania             1800      410000
## 3 Algeria             1800     2500000
## 4 Andorra             1800        2650
## 5 Angola              1800     1570000
## 6 Antigua and Barbuda 1800       37000
#Merging LifeExpIncome data set with population_total data set.

lifeE_geo_income <- merge(life_ex_income,L_populationT,
by.x= c("geo","year"),
by.y = c("geo","year"))


head(lifeE_geo_income)
##           geo year Income LifeExp region Population
## 1 Afghanistan 1800    603    28.2   Asia    3280000
## 2 Afghanistan 1801    603    28.2   Asia    3280000
## 3 Afghanistan 1802    603    28.2   Asia    3280000
## 4 Afghanistan 1803    603    28.2   Asia    3280000
## 5 Afghanistan 1804    603    28.2   Asia    3280000
## 6 Afghanistan 1805    603    28.2   Asia    3280000

Renaming Columns of LifeExp.Income.Region.Pop data set and converting data set to a tibble.

lifeE_geo_income <-  lifeE_geo_income %>% rename(Country=geo,Year=year,Region=region)


head(lifeE_geo_income)
##       Country Year Income LifeExp Region Population
## 1 Afghanistan 1800    603    28.2   Asia    3280000
## 2 Afghanistan 1801    603    28.2   Asia    3280000
## 3 Afghanistan 1802    603    28.2   Asia    3280000
## 4 Afghanistan 1803    603    28.2   Asia    3280000
## 5 Afghanistan 1804    603    28.2   Asia    3280000
## 6 Afghanistan 1805    603    28.2   Asia    3280000
lifeE_geo_income <- as_tibble(lifeE_geo_income)

#chaning type here
data.class(lifeE_geo_income)
## [1] "tbl_df"
lifeE_geo_income_in2015 <- lifeE_geo_income %>% filter(Year=="2015")


head(lifeE_geo_income_in2015)
## # A tibble: 6 x 6
##   Country             Year  Income LifeExp Region   Population
##   <chr>               <chr>  <dbl>   <dbl> <chr>         <dbl>
## 1 Afghanistan         2015    1750    57.9 Asia       33700000
## 2 Albania             2015   11000    77.6 Europe      2920000
## 3 Algeria             2015   13700    77.3 Africa     39900000
## 4 Andorra             2015   46600    82.5 Europe        78000
## 5 Angola              2015    6230    64   Africa     27900000
## 6 Antigua and Barbuda 2015   20100    77.2 Americas      99900
plot_ly(
  data = lifeE_geo_income_in2015,
  x = ~LifeExp, 
  y = ~Income,   
  color = ~Region, 
  text = ~Country,  
  alpha  = 0.5,
  size = ~Population,
  type = "scatter",
  mode = "markers"
) %>%
  layout(showlegend = FALSE)
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.
lifeE_geo_income$Year <- as.numeric(lifeE_geo_income$Year)


head(lifeE_geo_income)
## # A tibble: 6 x 6
##   Country      Year Income LifeExp Region Population
##   <chr>       <dbl>  <dbl>   <dbl> <chr>       <dbl>
## 1 Afghanistan  1800    603    28.2 Asia      3280000
## 2 Afghanistan  1801    603    28.2 Asia      3280000
## 3 Afghanistan  1802    603    28.2 Asia      3280000
## 4 Afghanistan  1803    603    28.2 Asia      3280000
## 5 Afghanistan  1804    603    28.2 Asia      3280000
## 6 Afghanistan  1805    603    28.2 Asia      3280000
p <- ggplot(lifeE_geo_income, aes(x = Income, 
                           y=LifeExp, 
                           size = Population, 
                           colour = Region)) + #Use different colors for different regions.
        geom_point(aes(size = Population, ids = Country ),
                   show.legend = FALSE, 
                   alpha = 0.7) +
        scale_color_viridis_d() + 
        scale_x_log10() +
        labs(title = 'Year: {frame_time}',
             x = "Life Expectancy", 
             y = "Income") +
       transition_time(Year)+
  ease_aes('linear')
## Warning: Ignoring unknown aesthetics: ids
## 
anim_save("LifeExp.gif", p)
## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 3 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).
  animate(p, renderer = gifski_renderer())  # this command will pop-up a new graphic window showing the animation.
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 9 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 3 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).